package com.qiang.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 功能描述: <br>
 * 〈线程池〉
 * @param:
 * @return:
 * @author: zhuhongqiang3
 * @date: 2020/6/17 18:06
 */
public class ThreadTaskPoolExecutor {
    public static void main(String[] args) throws InterruptedException {
        int count = 100;
        List list = Collections.synchronizedList(new ArrayList());
        CountDownLatch countDownLatch = new CountDownLatch(count);
        ExecutorService executorService = Executors.newFixedThreadPool(50);
        for (int i = 0; i < count; i++) {
            int finalI = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        list.add(finalI);
                    } finally {
                        countDownLatch.countDown();
                    }
                }
            });
        }
        countDownLatch.await();
        System.out.println("list: "+list.size());
        executorService.shutdown();
    }
}
